'use strict';

const sidelist = [
    {name: "面板", url: "dashboard", init: {icon: "icon-061"}, id: 0},
    {name: "文章", url: "article", init: {icon: "icon-061"}, id: 0},
    {name: "用户", url: "guest", init: {icon: "icon-033"}, id: 1},
    {name: "导航菜单", url: "navmenu", init: {icon: "icon-035"}, id: 3},
    // {name:"插入代码",url:"code-injection",init:{icon:"icon-004"},id:3},
    {name: "主题市场", url: "theme-market", init: {icon: "icon-046"}, id: 3},
    {name: "设置", url: "setting", init: {icon: "icon-034"}, id: 2},
    {name: "数据库", url: "datebase", init: {icon: "icon-004"}, id: 2},
]

module.exports = app => {
    class WxingController extends app.Controller {
        * detail() {
            var _tpl = this.ctx.params.detailid == undefined ? "dashboard" : this.ctx.params.id + "-detail",
                columnNames, fileInfo,_localurl="http://"+this.ctx.request.header.host;

            switch (_tpl) {
                case "datebase-detail":
                    columnNames = yield this.ctx.service.getDatebase.datebase.findFields(this.ctx.params.detailid);
                    //console.log("datebaseInfo", JSON.stringify(columnNames));
                    break;
                case "datebase":
                    fileInfo = yield this.ctx.service.getDatebase.datebase.findAll();
                    break;
            }

            const wxing_data = {
                column:{
                    name:"table-"+this.ctx.params.detailid,
                    list:columnNames
                }
            }
            yield this.ctx.render('admin/default', {
                tpl: {
                    html: _tpl,
                    active_nav: this.ctx.params.id,
                    sidelist: sidelist,
                    baseurl: _localurl,
                }, wxing_data: wxing_data
            });
        }

        * index() {
            var _tpl = this.ctx.params.id == undefined ? "dashboard" : this.ctx.params.id;
            var datebaseInfo, fileInfo,posts,
                _localurl="http://"+this.ctx.request.header.host,
                _useragent=this.ctx.request.header["user-agent"];
            

            switch (this.ctx.params.id) {
                case "datebase":
                    datebaseInfo = yield this.ctx.service.getDatebase.datebase.findAll();
                    break;
                case "article":
                    posts = yield this.ctx.service.getDate.posts.findAll();
                    break;
            }
            const wxing_data = {
                datebaseInfo: datebaseInfo,
                fileInfo: fileInfo,
                posts: posts,
            }
            //console.log('wxing_data',JSON.stringify(wxing_data))
            yield this.ctx.render('admin/default', {tpl: {
                html: _tpl,
                active_nav: this.ctx.params.id,
                sidelist: sidelist,
                baseurl: _localurl,
                useragent: _useragent,
            }, wxing_data: wxing_data});
        }
    }
    return WxingController;
};